<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\user;

use app\api\model\dao\RecommendRank;
use app\api\model\dao\UserAccount;
use app\api\model\dao\UserCurrency;
use app\library\Common;
use app\library\CommonConfig;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;

class GetRecommendRank extends PageBase
{

    private $objRecommendRank;
    private $objUserAccount;
    private $objUserCurrency;

    public function __construct()
    {
        parent::__construct();

        $this->objRecommendRank = new RecommendRank();
        $this->objUserAccount = new UserAccount();
        $this->objUserCurrency = new UserCurrency();

    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function myExcute($param)
    {
        $this->data = $this->getRecommendRank();

    }

    /**
     * 推举榜
     *
     * @param $cir_id
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function getRecommendRank()
    {
        $ret = $this->objRecommendRank->getRecommendRank($this->offset, $this->limit);

        if (empty($ret)) {
            return [];
        }

        $userid = [];
        $userid2Rank = [];

        foreach ($ret as $val) {
            $userid[] = $val['userid'];
            $userid2Rank[$val['userid']] = $val;
        }

        $user_account = [];
        $user_integral = [];

        if (!empty($userid)) {
            $user_account_ret = $this->objUserAccount->getUserAccountByUserids($userid, ['id', 'nick_name', 'sex', 'level', 'avatar', 'label']);
            if (empty($user_account_ret)) {
                return [];
            }
            $user_account_ret = Common::modelToArray($user_account_ret);
            foreach ($user_account_ret as $user) {
                $user_account[$user['id']] = $user;
            }
            $user_integral_ret = $this->objUserCurrency->getCurrencyByUserids($userid, Flag::CURENCY_TYPE_INTEGRAL);
            if (!empty($user_integral_ret)) {
                $user_integral_ret = Common::modelToArray($user_integral_ret);
            } else {
                $user_integral_ret = [];
            }
            foreach ($user_integral_ret as $integral) {
                $user_integral[$integral['user_id']] = $integral['amount'];
            }
        }


        $data = [];

        foreach ($userid2Rank as $key => $val) {
            if (empty($user_account[$key])) {
                continue;
            }
            $label = json_decode($user_account[$key]['label'], true);
            $integral = !empty($user_integral[$key]) ? $user_integral[$key] : 0;
            $level = Common::conversionLevel($integral);
            $data[] = [
                'userid' => $user_account[$key]['id'],
                'name' => $user_account[$key]['nick_name'],
                'photo' => $user_account[$key]['avatar'] ? $user_account[$key]['avatar'] : CommonConfig::DEFAULT_AVATAR,
                'sex' => 1 == $user_account[$key]['sex'] ? 'F' : 'M',
                'level' => $level,
                'label' => $label,
                'reason' => '推荐理由：' . $val['reason']
            ];
        }

        return $data;

    }

}
